var originWidth:Number;
var originHeight:Number;

var minWidth:Number;
var minHeight:Number;

var zoom:Number;
var x:Number;
var y:Number;
var height:Number;
var width:Number;

var cropOffsetX:Number;
var cropOffsetY:Number;

var editTarget:MovieClip;
var mouseWheelObj:Object;

function initializeCoverSize()
{
	// var clipCover=Packages.system.mlf.GateKeeper.scanElement(this._name, "clipCover");
	// minWidth=clipCover._width;
	// minHeight=clipCover._height;
	minWidth=120;
	minHeight=150;
}

function loadCover2Clip(container:String, uuid:String)
{
	// Packages.log("loadCover2Clip...", "debug");
	var clipCover=Packages.system.mlf.GateKeeper.scanElement(this._name, container);
	if (clipCover.cover!=null)
	{
		clipCover.cover.removeMovieClip();
	}
	var mclListener=new Object();
	mclListener.owner=this;
	mclListener.onLoadInit=function(target_mc:MovieClip)
	{
		this.owner.onAdjustImage(target_mc);
	}

	var src=getResourceDownloadUrl()+"?uuid="+uuid+"&type=3&"+"subtype="+subtype+"&sessionId="+Packages.model.Context.sessionID;
	 Packages.log("download..."+src, "debug");

	mlf.mcmanager.dynamicloading.LoadApp.loadAppLoader(src, clipCover, "cover", mclListener);
}

function onAdjustImage(target_mc:MovieClip)
{
	editTarget=target_mc;

	originWidth=target_mc._width;
	originHeight=target_mc._height;
	zoom=Packages.system.utils.ImageUtil.minSize(target_mc, minWidth, minHeight);

	var zoomValue=Packages.system.mlf.GateKeeper.scanElement(this._name, "zoomValue");
	zoomValue.text=zoom;

	refreshEditStage();
}

function initializeEditStage()
{
	x=0;
	y=0;
	width=120;
	height=150;
	cropOffsetX=0;
	cropOffsetY=0;

	refreshEditParam();
	
	var originSizeValue=Packages.system.mlf.GateKeeper.scanElement(this._name, "originSizeValue");
	originSizeValue.text=(originWidth?originWidth:"0")+"*"+(originHeight?originHeight:"0");
	loadCover2Clip("editStage", uuid2commit);
	
	mouseWheelObj=new Object();
	mouseWheelObj.owner=this;
	mouseWheelObj.onMouseWheel=function(delta)
	{
		if (delta>0)
		{
			this.owner.doZoom(true);
		}
		else 
		{
			this.owner.doZoom(false);
		}
	}
	Mouse.addListener(mouseWheelObj);
}

function disposeEditStage()
{
	Mouse.removeListener(mouseWheelObj);
}

function doZoom(flag:Boolean)
{
	var container=editTarget._parent;
	if (container._xmouse<0 || container._xmouse>minWidth || container._ymouse<0 || container._ymouse>minHeight)
	{
		return;
	}
	
	if (flag)
	{
		if (zoom<=490)
		{
			zoom+=10;
		}
	}
	else
	{
		if (zoom>=10)
		{
			zoom-=10;
		}
	}

	refreshEditStage();
}

function refreshEditStage()
{
	editTarget._xscale=zoom;
	editTarget._yscale=zoom;

	// centralize the target
	var offsetX:Number=(editTarget._width-minWidth)/2;
	editTarget._x=-offsetX;
	var offsetY:Number=(editTarget._height-minHeight)/2;
	editTarget._y=-offsetY;

	if (dynamicPortlet.bookInfo=="bookInfoProperties") return;

	// cropOffset
	var oldX:Number=x;
	var newX:Number=(minWidth-width)/2;
	cropOffsetX-=(newX-oldX)/zoom*100;
	var oldY:Number=y;
	var newY:Number=(minHeight-height)/2;
	cropOffsetY-=(newY-oldY)/zoom*100;

	editTarget._x-=cropOffsetX*zoom/100;		//
	editTarget._y-=cropOffsetY*zoom/100;		//

	x=newX;
	y=newY;

	refreshEditParam();
}

function refreshEditParam()
{
	var zoomValue=Packages.system.mlf.GateKeeper.scanElement(this._name, "zoomValue");
	zoomValue.text=zoom;
	var xValue=Packages.system.mlf.GateKeeper.scanElement(this._name, "xValue");
	xValue.text=x;;
	var yValue=Packages.system.mlf.GateKeeper.scanElement(this._name, "yValue");
	yValue.text=y;
	var widthValue=Packages.system.mlf.GateKeeper.scanElement(this._name, "widthValue");
	widthValue.text=width;
	var heightValue=Packages.system.mlf.GateKeeper.scanElement(this._name, "heightValue");
	heightValue.text=height;

	var topBar=Packages.system.mlf.GateKeeper.scanElement(this._name, "topBar");
	var bottomBar=Packages.system.mlf.GateKeeper.scanElement(this._name, "bottomBar");
	var leftBar=Packages.system.mlf.GateKeeper.scanElement(this._name, "leftBar");
	var rightBar=Packages.system.mlf.GateKeeper.scanElement(this._name, "rightBar");
	topBar._height=y+20;
	bottomBar._height=topBar._height;
	bottomBar._y=topBar._height+height;
	leftBar._width=x+20;
	leftBar._height=height;
	leftBar._y=topBar._height;
	rightBar._width=leftBar._width;
	rightBar._height=height;
	rightBar._x=leftBar._width+width;
	rightBar._y=leftBar._y;
}

function editParam(param, data, slot)
{
	var zoomValue=Packages.system.mlf.GateKeeper.scanElement(this._name, "zoomValue");
	zoom=Number(zoomValue.text);
	var xValue=Packages.system.mlf.GateKeeper.scanElement(this._name, "xValue");
	x=Number(xValue.text)?Number(xValue.text):0;
	x=Math.max(0, x);
	var yValue=Packages.system.mlf.GateKeeper.scanElement(this._name, "yValue");
	y=Number(yValue.text)?Number(yValue.text):0;
	y=Math.max(0, y);
	var widthValue=Packages.system.mlf.GateKeeper.scanElement(this._name, "widthValue");
	width=Number(widthValue.text)?Number(widthValue.text):0;
	width=Math.min(width, minWidth-x);
	width=Math.max(0, width);
	var heightValue=Packages.system.mlf.GateKeeper.scanElement(this._name, "heightValue");
	height=Number(heightValue.text)?Number(heightValue.text):0;
	height=Math.min(height, minHeight-y);
	height=Math.max(0, height);

	refreshEditStage();
}

function filterCoverByParams()
{
	calculateRectAfterZoom();
	
	var obj:Object=new Object();
	obj.uuid=uuid2commit.toString();
	obj.subtype=subtype.toString();
	obj.ratio=Math.round(zoom).toString();
	obj.x=x.toString();
	obj.y=y.toString();
	obj.height=height.toString();
	obj.width=width.toString();

	/*var data:Object=new Object();
	data.invokeCall="loadCover2Clip";
	data.invokeArgs=new Array();
	data.invokeArgs.push("clipCover");*/
	var data:Object=new Object();
	data.name="BookCreation";
	data.uri="app/content/bookPage/BookCreation.xml";
	data.returnPath="back2bookinfo";
	data.container="resultmode";
	data.oper="bookCoverFilter";

	Packages.system.display.Progress.createInstance(Packages.system.mlf.GateKeeper.scanElement(this._name, "resultmode"));

	Packages.modula.RequestCommand.request("REMOTING", "bookCoverFilter", obj, Packages.model.vo.CommonRemotingResultView, "normal", this, data);
}

function calculateRectAfterZoom()
{
	if (editTarget._x<0)
	{
		x=-editTarget._x+cropOffsetX*zoom/100;
	}
	else x=cropOffsetX*zoom/100;

	if (editTarget._y<0)
	{
		y=-editTarget._y+cropOffsetY*zoom/100;
	}
	else y=cropOffsetY*zoom/100;

	height=Math.min(editTarget._height, height);
	width=Math.min(editTarget._width, width);
}